﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Temperatures
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

if(system.platform=="windows") {
myPlugins["temp"] = new Object();
myPlugins["temp"]["name"] = getLocalizedString2("mod_sysm_plugin_temp_name");
myPlugins["temp"]["onlyonce"] = false;
myPlugins["temp"]["newObject"] = function (NewSettings) { return new plugin_temp_sensor(NewSettings); }
}

// --------------------------------------------------------------------------------
// Global

var TemperatureValues = new Array();
var TemperatureUpdate = 0;
var TemperatureSpeedFan = false;
var TemperatureSpeedFanCheck = -1;

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_temp_sensor(NewSettings) {
	// Required variables
	this.plugin = "temp";
	this.settings = NewSettings;
	this.edit = plugin_Edit;
	this.ReDraw = plugin_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#008000";
	if(!this.settings.updspeed) this.settings.updspeed = 5;
	if(!this.settings.tempsensorID) this.settings.tempsensorID = 0;
	if(!this.settings.tempsensorLabel) this.settings.tempsensorLabel = getLocalizedString2("mod_sysm_plugin_temp_name");


	//The Sonsore
	this.temps = new class_PercentBar(this.frame,0,0,this.settings.tempsensorLabel);
	this.temps.PercentColor(this.settings.color);
	this.temps.PercentBarColor(this.settings.barcolor);
	this.temps.PercentBarValue(0, "0 °C");


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_Update.call(obj); };
	plugin_Update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.temps.background.vOffset + this.temps.background.height;
	return true;


// --------------------------------------------------------------------------------
// Helper Functions


	function plugin_Edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		var TempOptions = new Array();
		var TempOptionsValues = new Array();
		for(var i = 0 ; i < TemperatureValues.length ; i++ ) {
			TempOptions[TempOptions.length] = getLocalizedString2("mod_sysm_plugin_temp_pref_SensorLabel") + i + " (" + TemperatureValues[i] + "°C)";
			TempOptionsValues[TempOptionsValues.length] = i;
		}

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_temp_pref_Sensor");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = TempOptions;
		Drive_Edit_DialogFields[FieldID].optionValue = TempOptionsValues;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.tempsensorID;
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_temp_pref_SensorDescription");

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_temp_pref_Label");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.tempsensorLabel;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(3,5,10,15,20,25,30);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;



		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.settings.tempsensorID = Drive_Edit_DialogResults[2];
			this.settings.tempsensorLabel = Drive_Edit_DialogResults[3];

			this.temps.PercentColor(this.settings.color);
			this.temps.PercentBarColor(this.settings.barcolor);
			this.temps.text_name.data = this.settings.tempsensorLabel;

			if ( this.settings.updspeed != Drive_Edit_DialogResults[4] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[4];
				this.timer.interval = this.settings.updspeed;
			}

			plugin_Update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}


	function plugin_Update() {
		plugin_GetTemps();

		if(TemperatureValues[this.settings.tempsensorID]) {
			this.temps.PercentBarValue(TemperatureValues[this.settings.tempsensorID], TemperatureValues[this.settings.tempsensorID] + " °C");
		} else {
			this.temps.PercentBarValue(100, getLocalizedString2("mod_sysm_plugin_offline"));
		}
		return true;
	}


	function plugin_ReDraw() {
		this.frame.width   = mod_sysmonitor_width;
		this.temps.style   = preferences.mod_sysmonitor_bar_style.value;
		this.temps.size    = mod_sysmonitor_size;
		this.frame.height  = this.temps.background.vOffset + this.temps.background.height;
		return true;
	}


	function plugin_GetTemps() {
		var updatetime = new Date();
		if ( TemperatureUpdate < (updatetime.getTime() - 2700) ) {
			TemperatureUpdate = updatetime.getTime();

			if(TemperatureSpeedFanCheck<0) {
				TemperatureSpeedFanCheck = 30;
				TemperatureSpeedFan = true;
			}

			if(TemperatureSpeedFan==true) {
				var tmp = runCommand(mod_sysmonitor_path_speedfan).split("\n");
				if(tmp.length==3) {
					TemperatureValues = tmp[2].split(" ");
				} else {
					TemperatureSpeedFan = false;
				}
			}

			if(TemperatureSpeedFan==false) {
				TemperatureSpeedFanCheck--;
				if(VBSEnabled) { //Fallback to WMI
					TemperatureValues = runCommand('cscript.exe //nologo "' + mod_sysmonitor_path_temperature + '"').split("\n");
				}
			}
		}
		return true;
	}


}

// --------------------------------------------------------------------------------
// The END